Amplify Utils
A library for solving common filesystem, path, and more.
Installation
npm i @axway/amplify-utils --save
fs
import {
existsSync,
isDir,
isFile,
locate
} from '@axway/amplify-utils';
console.log(existsSync('/path/to/something'));
console.log(isDir('/path/to/some/dir'));
console.log(isFile('/path/to/some/file'));
console.log(locate('/path/to/some/dir', 'somefile', 2));
path
import {
expandPath,
real
} from '@axway/amplify-utils';
console.log(expandPath('~/foo', 'bar'));
console.log(real('/some/path'));
utils
import { arch } from '@axway/amplify-utils';
console.log(arch());
import { arrayify } from '@axway/amplify-utils';
console.log(arrayify('foo'));
console.log(arrayify([ 'a', '', null, 'b' ], true));
import { assertNodeEngineVersion } from '@axway/amplify-utils';
assertNodeEngineVersion(require('package.json'));
import { cache } from '@axway/amplify-utils';
const now = () => Date.now();
const first = await cache('my namespace', now);
const second = await cache('my namespace', now);
assert(first === second);
const third = await cache('my namespace', true, now);
assert(first !== third && second !== third);
import { cacheSync } from '@axway/amplify-utils';
const now = () => Date.now();
const first = cacheSync('my namespace', now);
const second = cacheSync('my namespace', now);
assert(first === second);
const third = cacheSync('my namespace', true, now);
assert(first !== third && second !== third);
Debouncer that returns a promise and that can be cancelled.
import { debounce } from '@axway/amplify-utils';
const fn = debounce(() => {
console.log(new Date());
});
fn().then(() => {
console.log('Function called');
});
fn.cancel();
import { formatNumber } from '@axway/amplify-utils';
console.log(formatNumber(12));
console.log(formatNumber(123));
console.log(formatNumber(1234));
console.log(formatNumber(12345));
console.log(formatNumber(123456));
console.log(formatNumber(1234567));
import { get } from '@axway/amplify-utils';
const obj = {
foo: 'bar'
};
console.log(get(obj, 'foo'));
console.log(get(obj, 'baz', 'pow'));
Get all open sockets, [net] servers, timers, child processes, filesystem watchers, and other
handles.
import { getActiveHandles } from '@axway/amplify-utils';
console.log(getActiveHandles());
import { inherits } from '@axway/amplify-utils';
class A {}
class B extends A {}
class C {}
console.log(inherits(B, A));
console.log(inherits(B, C));
import { mergeDeep } from '@axway/amplify-utils';
const obj1 = {
a: {
b: 'c'
}
};
const obj2 = {
a: {
d: 'e'
}
};
console.log(mergeDeep(obj1, obj2));
import { mutex } from '@axway/amplify-utils';
const fn = () => {
return mutex('my mutex', () => {
console.log('foo!');
});
};
await Promise.all([ fn(), fn(), fn() ]);
import { randomBytes } from '@axway/amplify-utils';
console.log(randomBytes(20));
import { sha1 } from '@axway/amplify-utils';
console.log(sha1('foo'));
import { sleep } from '@axway/amplify-utils';
await sleep(1000);
Block multiple simultaneous callers until the first caller finishes, then all queued up 'tailgaters'
are resolved with the result.
import { tailgate } from '@axway/amplify-utils';
const fn = () => {
return tailgate('my tailgate', async () => {
console.log('I will only be called once');
});
};
await Promise.all([ fn(), fn(), fn() ]);
import { unique } from '@axway/amplify-utils';
console.log(unique([ 'a', 'b', 'a', 'b' ]));
Legal
This project is open source under the Apache Public License v2 and is developed by
Axway, Inc and the community. Please read the LICENSE
file included
in this distribution for more information.